import axios from 'axios'
import { useUserStore } from '@/stores'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
const router = useRouter()

const baseURL = 'http://big-event-vue-api-t.itheima.net'
const instance = axios.create({
    // TODO 1. 基础地址，超时时间
    baseURL,
    timeout: 5000,
})
instance.interceptors.request.use(
    config => {
        const userStore = useUserStore()
        // TODO 2. 携带token
        if (userStore.token) {
            config.headers.Authorization = userStore.token
        }
        return config
    },
    err => Promise.reject(err)
)

instance.interceptors.response.use(
    res => {
        // TODO 3. 处理业务失败
        // TODO 4. 摘取核心响应数据
        if (res.data.code === 0) {
            return res
        }
        ElMessage.error(res.data.message || '服务异常')

        return Promise.reject(res.data)
    },
    err => {
        if (err.response.status?.status === 401) {
            router.push('/login')
        }

        ElMessage.error(err.response.data.message || '服务异常')
        // TODO 5. 处理401错误
        return Promise.reject(err)
    }
)
export default instance
export { baseURL }